package com.overseasShopping.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.Biz.GoodsBiz;
import com.overseasShopping.Biz.OrderListBiz;
import com.overseasShopping.Biz.UserBiz;
import com.overseasShopping.pojo.Goods;
import com.overseasShopping.pojo.OrderList;
import com.overseasShopping.pojo.User;

 public class OrderAction extends ActionSupport{
	    private OrderListBiz orderListBiz;
	    private UserBiz userBiz;
	    private GoodsBiz goodsBiz;
	
		public GoodsBiz getGoodsBiz() {
			return goodsBiz;
		}
		public void setGoodsBiz(GoodsBiz goodsBiz) {
			this.goodsBiz = goodsBiz;
		}

		public OrderListBiz getOrderListBiz() {
			return orderListBiz;
		}
		public void setOrderListBiz(OrderListBiz orderListBiz) {
			this.orderListBiz = orderListBiz;
		}
		public UserBiz getUserBiz() {
			return userBiz;
		}
		public void setUserBiz(UserBiz userBiz) {
			this.userBiz = userBiz;
		}

		//判断是否登陆
		public void isLogin(){
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/plain;charset=UTF-8");
			HttpServletRequest request = ServletActionContext.getRequest();
			String uid = request.getParameter("uid");
			
			Map<String, Object> map = ActionContext.getContext().getSession();
			User user = (User) map.get("user");
						
			int temp = 0;
			if(user==null){
				temp = 1;
			}else if(uid.equals(user.getUserId())){
				temp = 2;
			}
			
			try {
				PrintWriter out = response.getWriter();
			    out.print(temp);
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void addOrder(){
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/plain;charset=UTF-8");
			String goodId = request.getParameter("goodId");	
			int count = Integer.parseInt(request.getParameter("count"));	
			long tel = Long.parseLong(request.getParameter("Tel"));	
			String address = request.getParameter("address");	
			
			Goods good = goodsBiz.getGoodBygoodId(goodId);
			User saleUser = userBiz.getUser(good.getDetailUser().getUserId());
			
			OrderList order = new OrderList();
			order.setCount(count);
			order.setTelephone(tel);
			order.setAddress(address);
			
			Map<String, Object> map = ActionContext.getContext().getSession();
			User buyUser = (User) map.get("user");
			
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String strdate = sdf.format(date);
			try {
				Date day = sdf.parse(strdate);
				order.setData(day);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			order.setState(1);
		    order.setBuyUser(buyUser);
		    order.setSaleUser(saleUser);
		    order.setPrice(good.getPrice());
		    order.setGoods(good);		    
		
			orderListBiz.addOrder(order);
			if(good.getCount()==order.getCount()){
				good.setState(3);
			}else{
		    	good.setCount(good.getCount()-order.getCount());			
			}
			goodsBiz.updateGoods(good);
			
			try {
				PrintWriter out = response.getWriter();
			    out.print("已生成訂單，請到個人中心查看付款");
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
}
